const {
  order
} = require("../../api/api");

// pages/orderSheet/applyAfterSale.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '',
    imgPath: "/images",

    //订单号
    order_sn: '',

    //订单
    order: null,

    //步骤
    steps: [{
        text: '步骤一',
        desc: '描述信息',
      },
      {
        text: '步骤二',
        desc: '描述信息',
      },
    ],
    //活动步骤
    activeStep: 0,

    //订单物流信息
    orderLogistics: [],

    //状态
    status: '',

    //物流单号
    logistics_nu: '',

    //物流地址
    address:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    // console.log("options", options)

    const util = require("../../utils/util")
    this.setData({
      imgPath: util.getImgPath()
    })

    //订单号
    let order_sn = options.order_sn;
    this.setData({
      order_sn: order_sn,
    })

    //取出订单列表
    let orderList = wx.getStorageSync('orderList');
    // console.log("orderList", orderList)

    //查找该订单
    let order = null;
    for (const orderTemp of orderList) {
      if (orderTemp.order_sn == order_sn) {
        order = orderTemp;
        break;
      }
    }
    if (order == null) {
      wx.showToast({
        title: '没发现订单',
      })
      return;
    }
    //将订单信息没到本地数据表
    this.setData({
      order: order,
    })

    //取得物流信息
    this.data.steps = [];
    const api = require("../../api/api");
    api.user.orderLogistics({
      order_sn: order_sn,
      type: 1,
      success(res) {
        // console.log("orderLogistics,res", res)

        let data2=res.data.data;

        //将物流信息保存到本地
        let rows = res.data.data.data;
        that.setData({
          orderLogistics: rows,
        })

        /*
        steps: [{
        text: '步骤一',
        desc: '描述信息',
        },
        */
        for (const row of rows) {
          that.data.steps.push({
            text: row.status + "(" + row.time + ")",
            desc: row.context,
          })
        }
        that.setData({
          steps: that.data.steps,
        })

        //状态
        if (rows.length > 0) {
          let lastRow = rows[0];
          // console.log("lastRow",lastRow)

          that.setData({
            status: lastRow.status,
          })
        }

        //物流单号
        that.setData({
          logistics_nu: order.logistics_nu==null?'':order.logistics_nu,
        })

        console.log('order.logistics_nu',order.logistics_nu);

        //收货地址
        that.setData({
          address:data2.receive_address,
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabbarChange(event) {
    // console.log("onTabbarChange,event", event)
    this.setData({
      active: event.detail
    })
  },
})